<?php

class User extends AppModel {

    var $name = 'User';

    /**
     * User type
     */
    const USER_PREFIX_ITS = 'its';
    const USER_PREFIX_SUPPLIER = 'supplier';
    const USER_PREFIX_CUSTOMER = 'customer';

    var $hasOne = array('UserIts', 'UserSupplier', 'UserCustomer');
    
    var $hasManay = array('UserCoH');
    
    var $validate = array(
        'username' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Please input a username.',
                'on' => 'create',
                'required' => true,
            ),
            'isUnique' => array(
                'rule' => 'isUnique',
                'on' => 'create',
                'message' => 'This username is already taken.',
            ),
        ),
        'name' => array(
            'rule' => 'notEmpty',
            'message' => 'Please input a name.',
            'required' => true,
        ),
        'surname' => array(
            'rule' => 'notEmpty',
            'message' => 'Please input a surname.',
            'required' => true,
        ),
        'password' => array(
            'rule' => 'notEmpty',
            'message' => 'Please input a password.',
        ),
        'password_confirm' => array(
            'rule' => 'notEmpty',
            'message' => 'Please input a password.',
        ),
        'prefix' => array(
            'rule' => 'notEmpty',
            'message' => 'Please select a user type.',
            'required' => true,
        ),
    );

    function beforeSave() {
        if (isset($this->data['User']['birth_date'])) {
            $this->data['User']['birth_date'] = $this->dateFormatBeforeSave($this->data['User']['birth_date']);
        }
        return true;
    }

    function afterFind($results) {
        foreach ($results as &$user) {
            if (isset($user['User']['birth_date'])) {
                $user['User']['birth_date'] = $this->dateFormatAfterFind($user['User']['birth_date']);
            }
        }
        return $results;
    }

    /**
     * ดึง list Email User ของ Vendor ทั้งหมดที่ดูแลลูกค้าที่ระบุ
     * @param type $customer_id
     * @param type $supplier_id 
     */
    function listEmailUserSupplierByCustomer($customer_id, $supplier_id) {
        // ดึง list user ของ Supplier ที่กำหนดทั้งหมด
        $userSuppliers = $this->UserSupplier->find('list', array('fields' => array('user_id', 'user_id'), 'conditions' => array('supplier_id' => $supplier_id)));

        App::import('Model', 'UserCoH');
        $UserCoH = new UserCoH();
        
        // ส่ง Email หา User ที่ดูแล Customer On Hand
        $UserCoH->contain();
        $userCoHs = $UserCoH->find('list', array(
                    'fields' => array('user_id', 'user_id'),
                    'conditions' => array(
                        'UserCoH.customer_id' => $customer_id,
                        'UserCoH.user_id' => $userSuppliers,
                    )
                ));

        $users = $this->find('list', array('fields' => array('email'), 'conditions' => array('id' => $userCoHs)));
//        debug($users);
        return $users;
    }

}

?>
